package top.yuki.event;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerExpChangeEvent;
import top.yuki.Chat.ChatCharacteristic;
import top.yuki.LevelWorld;
import top.yuki.data.DataManager;
import top.yuki.data.DayDataManager;
import top.yuki.data.TempDataManage;

public class onPlayerExpChange implements Listener {

    public DataManager dataManage;
    private TempDataManage tempDataManage;
    private DayDataManager dayDataManager;
    private ConfigurationSection config;
    public onPlayerExpChange (LevelWorld lw){
        this.dataManage = lw.dataManage;
        this.tempDataManage = lw.tempDataManage;
        this.dayDataManager = lw.dayDataManager;
        this.config = lw.getConfig();
    }

    @EventHandler
    public void onPlayerExpChange(PlayerExpChangeEvent e){
        Player p = e.getPlayer();
        String id = String.valueOf(p.getUniqueId());

        int dayMaxExp = (int) config.get("dayMaxExp."+dataManage.getWorldLevel(p.getUniqueId()));
        int minecraftLevel = p.getLevel();

        if (minecraftLevel >= dataManage.calMaxMinecraftLevel(p.getUniqueId())){
            e.setAmount(0);
            if (tempDataManage.getExpLimitRemind(String.valueOf(p.getUniqueId()))) {
                p.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"经验已达上限，请升级世界(本日不再提醒)");
                tempDataManage.updateExpLimitRemind(String.valueOf(p.getUniqueId()),false);
            }

        }else if (dayDataManager.getDayGetExp(id)>dayMaxExp){
            e.setAmount(0);
            if (tempDataManage.getDayExpLimitRemind(String.valueOf(p.getUniqueId()))) {
            p.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"本日经验已达上限(升级世界获得更高经验上限，本日不再提醒)");
            tempDataManage.updateDayExpLimitRemind(String.valueOf(p.getUniqueId()),false);
            }
        }else{
            int amount = e.getAmount();
            dayDataManager.addDayGetExp(id,amount);

        }

    }

}
